// let fn = function (resolve, reject) {

//     console.log('begin to execute');

//     let number = Math.random();

//     if (number < 0.5) {
//         resolve('less than 0.5');
//     } else {
//         reject('big than 0.5');
//     }

// }

// let p = new Promise(fn);

// p.then(function (res) {
//     console.log('resolve ' + res);
// }, function (err) {
//     console.log('reject ' + err);
// }).catch(function (err) {
//     console.log(err);
// });

//手动实现简单的promise
{

    function MyPromise(fn) {

        this.value;
        this.status = 'pending';
        this.resolveFunc = function () {};
        this.rejectFunc = function () {};

        console.log(this.resolve);

        console.log(fn);

        fn(this.resolve.bind(this), this.reject.bind(this));
    }

    MyPromise.prototype.resolve = function (val) {

        let slef = this;

        if (self.status === 'pending') {
            self.status = 'resolved';
            self.value = val;
            setTimeout(function () {
                self.resolveFunc(self.value);
            }, 0);
        }
    }

    MyPromise.prototype.reject = function (val) {

        let self = this;

        if (self.status === 'pending') {
            self.status = 'rejected';
            self.value = val;
            setTimeout(function () {
                self.rejectFunc(self.value);
            }, 0);
        }
    }

    MyPromise.prototype.then = function (resolveFunc, rejectFunc) {

        let self = this;

        this.resolveFunc = resolveFunc;
        this.rejectFunc = rejectFunc;

        // return new MyPromise(function (resolve_next, reject_next) {

        //     function resolveFuncWrap() {
        //         let result = resolveFunc(self.value);

        //         if (result && typeof result.then === 'function') {
        //             result.then(resolve_next, reject_next);
        //         } else {
        //             resolve_next(result);
        //         }
        //     }

        //     function rejectFuncWrap() {
        //         let result = rejectFunc(self.value);
        //         if (result && typeof result.then === 'function') {
        //             result.then(resolve_next, reject_next);
        //         } else {
        //             resolve_next(result);
        //         }
        //     }

        //     this.resolveFunc = resolveFuncWrap;
        //     this.rejectFunc = rejectFuncWrap;
        // });
    }

    let p = new MyPromise(function (resolve, reject) {

        let number = Math.random;

        if (number > 0.2) {

            resolve('big than 0.5');

        } else {

            reject('less than 0.5');
        }
    });


    p.then(function (res) {
        console.log('resolve ' + res);
    }, function (res) {
        console.log('reject ' + res);
    });

}
{

    try {

        console.log(a+5);
    }catch(e){

        console.log(e)

        window.location.href = 'http://www.baidu.com?err+' + e

    }

}